// pages/user/coin.js
import { User } from '../../model/user'
import { UserCurrency } from '../../model/user-currency'
import { System } from '../../model/system'
import { Pay } from "../../model/pay";
const appInst =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo: null,
      pay_loading: false,
      system: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    });
    this.getSystem();
  },
  async getSystem() {
    const res = await System.getInfo();
    this.setData({
      system: res
    })
    this.getUserInfo();
  },
  async getUserInfo() {
    const res = await User.getUserInfo();
    this.setData({
      userInfo: res
    });
    wx.hideLoading();
  },
  openCoinList() {
    wx.navigateTo({
      url: 'coin_list',
    });
  },
  /**
   * 创建支付订单
   */
  async createOrder(e) {
    const dataset = e.currentTarget.dataset;
    if(this.data.pay_loading) {
        return false;
    }
    wx.showLoading({
      title: '支付中',
    });
    this.setData({
        pay_loading: true,
    })
    const res = await UserCurrency.createOrder({give: dataset.give, itself: dataset.itself, price: dataset.price });
    if(res) {
      appInst.preOrder(res.out_trade_no);
    }
  },
  /**
   * 打开会员权益
   */
  openUserPay() {
    wx.navigateTo({
      url: 'user_pay',
    });
  },
  /**
   * 打开认证页面
   */
  openAuth() {
    wx.navigateTo({
      url: 'auth',
    });
  },
  /**
   * 打开活动页面
   */
  openActivity() {
    wx.switchTab({
      url: '../blind_date/blind_date'
    })
  },
  /**
   * 打开我要上墙页面
   */
  openBlindDate() {
    wx.navigateTo({
      url: 'blind_date',
    });
  },
  /**
   * 打开隐私设置页面
   */
  openPrivacy() {
    wx.navigateTo({
      url: 'privacy',
    });
  },
  onShareAppMessage(res) {
      if(res.form === 'button') {
        // 页面内的转发
        console.log(res.target,res);
      }
      return {
        path: 'page/home/home?openid='+this.data.userInfo.openid,
      };
  }
})